var page=0;
var sum=0;
// var button='<button onclick="Key.keyword_list(0)" class="layui-btn layui-btn-primary layui-btn-sm" style="margin-top: 21%;margin-left: 20%;"><<返回</button>';
$(function(){
    if(localStorage.getItem("IS_ADMIN")==0){
        $(".btn_add").hide();
    }
    Key.keyword_list(0);
    Key.limit();
})
var Key={
    keyword_list:function(e,pageSize){
        html="";
        if(e==undefined){
            e=0;
        }else if(pageSize==undefined){
            pageSize=10;
        }
        $.ajax({
            type:'POST',
            url:request('/console/queryRecordsNew')/*+"&access_token="+localStorage.getItem("access_Token")*/,
            data:{
                pageIndex:(e==0?"0":e-1),
                pageSize:pageSize
            },
            dataType:'json',
            async:false,
            success:function(result){
                if(result.count.length!=0){
                    $("#pageCount").val(result.count);
                    for(var i=0;i<result.data.length;i++){
                        var td;
                        var status = result.data[i].applyStatus;
                        if (status == 0){
                            td = "<button onclick='Key.audit(\""+result.data[i].id+"\")' class='layui-btn layui-btn-danger layui-btn-xs delete'>审核</button>";
                        } else if(status == 1) {
                            td = "审核通过"
                        }else if(status == 2) {
                            td = "未通过"
                        }else{
                        	 td = result.data[i].auditMsg;
                        }
                        html+="<tr align='center'>" +
                        "<td>"+result.data[i].userId+"</td>" +
                        "<td>"+result.data[i].userName+"</td>" ;
                        
                        	 html+="<td>"+result.data[i].bankCart+"</td>" +
                            "<td>"+result.data[i].name+"</td>" +
                            "<td>"+result.data[i].openingBank+"</td>" ;
                        
                        html+= "<td>"+result.data[i].serviceTime+"</td>" +
                        "<td>"+result.data[i].amount+"</td>" +
                        "<td>"+result.data[i].rate+"</td>" +
                        "<td>"+result.data[i].sjtxje+"</td>" +
                        "<td>"+result.data[i].balance+"</td>" +
                        "<td>"+td+"</td>" +
                        "</tr>";
                    }
                    $("#keywordList_table").empty();
                    $("#keywordList_table").append(html);
                    if(localStorage.getItem("IS_ADMIN")==0){
                        $(".delete").hide();
                    }
                    $("#keyWordList").show();
                    $("#addKeyWord").hide();
                    // $("#back").empty();
                    // $("#back").append("&nbsp;");

                }
            }
        })
    },
    // 搜索关键词
    findKeyWord:function(){
        html="";
        myFn.invoke({
            type:'POST',
            url:request('/console/queryRecordsNew')/*+"&access_token="+localStorage.getItem("access_Token")*/,
            data:{
                userId:$("#userId").val()
            },
            dataType:'json',
            async:false,
            success:function(result){
            	//debugger;
                if(result.count.length!=0){
                    $("#pageCount").val(result.count);

                    for(var i=0;i<result.data.length;i++){
                        var td;
                        var status = result.data[i].applyStatus;
                        if (status == 0){
                            td = "<button onclick='Key.audit(\""+result.data[i].id+"\")' class='layui-btn layui-btn-danger layui-btn-xs delete'>审核2</button>";
                        } else if(status == 1) {
                            td = "审核通过"
                        }else if(status == 2) {
                            td = "未通过"
                        }else{
                        	 td = result.data[i].auditMsg;
                        }
                        html+="<tr align='center'>" +
                        "<td>"+result.data[i].userId+"</td>" +
                        "<td>"+result.data[i].userName+"</td>" ;
                        
                        	 html+="<td>"+result.data[i].bankCart+"</td>" +
                            "<td>"+result.data[i].userName+"</td>" +
                            "<td>"+result.data[i].openingBank+"</td>" ;
                        
                        html+= "<td>"+result.data[i].serviceTime+"</td>" +
                        "<td>"+result.data[i].sjtxje+"</td>" +
                        "<td>"+result.data[i].rate+"</td>" +
                        "<td>"+result.data[i].amount+"</td>" +
                        "<td>"+result.data[i].balance+"</td>" +
                        "<td>"+td+"</td>" +
                        "</tr>";
                    }
                    $("#keywordList_table").empty();
                    $("#keywordList_table").append(html);
                    if(localStorage.getItem("IS_ADMIN")==0){
                        $(".delete").hide();
                    }
                    $("#keyWordList").show();
                    $("#addKeyWord").hide();
                    // $("#back").empty();
                    // $("#back").append("&nbsp;");

                }
            }
        })
    },
    audit:function(id){
    	$that = this;
    	layer.confirm('是否确认通过？', {
    		  btn: ['通过', '驳回'] //可以无限个按钮
		}, function(index, layero){
			$that.submitAudit(1,id,index);
		}, function(index){
			$that.submitAudit(0,id,index);
		});
    },submitAudit:function(status,id,index){
    	 $.ajax({
             url:request('/console/audit')/*+"&access_token="+localStorage.getItem("access_Token")*/,
             data:{
                 id:id,
                 status:status,
                 auditMsg:"审核通过"
             },
             success:function(result){
             	//debugger;
                 if(result.resultCode==1){
                     layer.alert("审核成功");
                     Key.keyword_list();
                     Key.limit();
                     layer.close(index);
                 } else {
                     layer.alert(result.data);
                     Key.keyword_list();
                     Key.limit();
                     layer.close(index);
                 }
             }
         });
    },
    // 分页
    limit:function(){
        layui.use('laypage', function(){
            var laypage = layui.laypage;
            console.log($("#pageCount").val());
            var count=$("#pageCount").val();
            //执行一个laypage实例
            laypage.render({
                elem: 'laypage'
                ,count: count
                ,layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip']
                ,jump: function(obj){
                    console.log(obj)
                    Key.keyword_list(obj.curr,obj.limit)
                }
            })
        })
    }
}